{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Step 2: Advanced Self Driving Dataset Techniques\n",
    "\n",
    "Let's begin looking at some advance methods we can do with our self driving dataset by flattening our group dataset. By default, a grouped dataset returns only one active slice when you grab a sample. If you want to look at all cameras at once, we can call select_group_slices and pass a media type or a list of group names to get the flattened samples back.\n",
    "\n",
    "Run the code below if you have not run [Loading Dataset](<01_loading_datasets.ipynb>), otherwise you can use the dataset from before! "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import fiftyone as fo\n",
    "import fiftyone.zoo as foz\n",
    "\n",
    "# Run if you skipped Step 1\n",
    "all_sensor_dataset = foz.load_zoo_dataset(\"quickstart-groups\")\n",
    "\n",
    "# Run if you completed Step 1\n",
    "all_sensor_dataset = fo.load_dataset(\"nuscenes_sensors\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get all the image samples from the dataset\n",
    "flattened_dataset = all_sensor_dataset.select_group_slices(media_type=\"image\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Leveraging Embeddings\n",
    "One of the strongest ways we can interact with our self driving car dataset is by using embeddings. By utilizing the FiftyOne Brain, we can can compute embedddings on our dataset and do some awesome things. Let's check them out!\n",
    "\n",
    "### Compute Visualization\n",
    "By computing the visualization of the embeddings on our dataset, we can map the distribution of our dataset into a 2D space for us to analyze and curate! This works great on AV datasets as you can \"see\" the individual drives on each sensor as well as where they deviate. Let's take a look:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import fiftyone.brain as fob\n",
    "\n",
    "results = fob.compute_visualization(\n",
    "    flattened_dataset,  \n",
    "    brain_key=\"embedding_viz\",\n",
    "    model=\"clip-vit-base32-torch\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compute Similarity\n",
    "Computing similarity is another awesome way to get more insights into your dataset, especially if you are trying to find the right scenario! By computing similarity with a vision-language model, you can use semantic search to find scenarios like \"rainy crosswalks\", \"pedestrians in the road\", or more!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fob.compute_similarity(\n",
    "    flattened_dataset,\n",
    "    model=\"clip-vit-base32-torch\",\n",
    "    brain_key=\"img_sim\",\n",
    ")\n",
    "\n",
    "session = fo.launch_app(flattened_dataset)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![compute_similarity](https://cdn.voxel51.com/getting_started_self_driving/notebook1/compute_similarity2.webp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also use the FiftyOne Brain to find :\n",
    "\n",
    "- Most unique samples\n",
    "- Likely label mistakes\n",
    "- Your hardest samples\n",
    "- Most Representative Samples\n",
    "\n",
    "## Using the Model Zoo\n",
    "\n",
    "We can use a ton of models right out of the box to get more insights into our dataset. Utilizing the FiftyOne Model Zoo, we can load in some models to bring even more information to help with curation or downstream tasks. Let's try one of the most popular models right now in Meta's SAM2!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# SAM2 Install Instructions:\n",
    "\n",
    "!git clone https://github.com/facebookresearch/sam2.git && cd sam2\n",
    "\n",
    "!pip install -e ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import fiftyone.zoo as foz\n",
    "\n",
    "model = foz.load_zoo_model(\"segment-anything-2-hiera-tiny-image-torch\")\n",
    "# Prompt with boxes\n",
    "flattened_dataset.apply_model(\n",
    "    model,\n",
    "    label_field=\"sam2\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Aftwerwards, you will be able to see some semantic segmentations of our image of the best things SAM2 decided to segment. You can also prompt SAM2 with your own bounding boxes for greater accuracy. If you aren't happy with the accuracy, don't worry, we will be using an even better model later!\n",
    "\n",
    "Let's try using a depth model to help us determine how far away other cars and objects are in our image samples!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = foz.load_zoo_model(\"depth-estimation-transformer-torch\")\n",
    "\n",
    "flattened_dataset.apply_model(model, label_field=\"depth\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each of our samples in our dataset now have a depth heatmap and a semantic sgementation on them. You can store as many different label types as you like on any sample in FiftyOne! By leveraging FiftyOne Model Zoo and the FiftyOne Brain, you can greatly enhance any self driving workflow you do!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "OSS310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
